﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpMikeCtrl;

namespace USBDeviceCtrl
{
    public class PhilipsManager
    {
        SpeechMikeControlClass deviceController;
        int unitID = 0;

        public delegate void ButtonHandler(object sender, buttonArgs args);
        public event ButtonHandler eventButton;

        public delegate void ConnectionHandler(object sender, buttonArgs args);
        public event ConnectionHandler deviceConnected;

        public delegate void DisconnectionHandler(object sender, buttonArgs args);
        public event DisconnectionHandler deviceDisconnected;

        public PhilipsManager()
        {
            deviceController = new SpeechMikeControlClass();
            deviceController.SPMEventDeviceConnected += new _DSpeechMikeControlEvents_SPMEventDeviceConnectedEventHandler(deviceController_SPMEventDeviceConnected);
            deviceController.SPMEventDeviceDisconnected += new _DSpeechMikeControlEvents_SPMEventDeviceDisconnectedEventHandler(deviceController_SPMEventDeviceDisconnected);
            deviceController.SPMEventButton += new _DSpeechMikeControlEvents_SPMEventButtonEventHandler(deviceController_SPMEventButton);
            deviceController.Initialize(false);
            deviceController.Activate();
        }        

        void deviceController_SPMEventButton(int lDeviceID, spmControlDeviceEvent EventId)
        {
            if (unitID != lDeviceID)
            {
                unitID = lDeviceID;
                CSpmDeviceInfo devInfo = deviceController.get_DeviceInfoFromId(lDeviceID);
                int num = devInfo.LFHNumber;
                if (num > 5000 && num < 5999)
                    deviceConnected(this, new buttonArgs(lDeviceID.ToString()));
            }

            switch (EventId)
            {
                case spmControlDeviceEvent.spmPlayPressed:
                    eventButton(this, new buttonArgs("Play"));
                    break;
                case spmControlDeviceEvent.spmPlayStopTogglePressed:
                    eventButton(this, new buttonArgs("PlayStopToggle"));
                    break;
                case spmControlDeviceEvent.spmRecordPressed:
                    eventButton(this, new buttonArgs("Record"));
                    break;
                case spmControlDeviceEvent.spmStopPressed:
                    eventButton(this, new buttonArgs("Stop"));
                    break;
                case spmControlDeviceEvent.spmInsertPressed:
                    eventButton(this, new buttonArgs("Insert"));
                    break;
                case spmControlDeviceEvent.spmEOLPressed:
                    eventButton(this, new buttonArgs("EOL"));
                    break;
                case spmControlDeviceEvent.spmFastForwardPressed:
                    eventButton(this, new buttonArgs("FForwardPressed"));
                    break;
                case spmControlDeviceEvent.spmFastRewindPressed:
                    eventButton(this, new buttonArgs("FRewindPressed"));
                    break;
                case spmControlDeviceEvent.spmFastForwardReleased:
                    eventButton(this, new buttonArgs("FRewindReleased"));
                    break;
                case spmControlDeviceEvent.spmFastRewindReleased:
                    eventButton(this, new buttonArgs("FRewindReleased"));
                    break;
            }
        }

        public bool IsRecordConnected
        {
            get 
            {
                bool result = false;
                int numdevices = deviceController.get_NumberOfDevices(0);
                CSpmDeviceInfo device;
                for (int i = 0; i < numdevices; i++)
                {
                    device = (CSpmDeviceInfo)deviceController.get_DeviceInfo(i);
                    if (device.LFHNumber > 5000 && device.LFHNumber < 5999)
                    {
                        result = true;
                    }
                }
                return result;
            }
        }

        void deviceController_SPMEventDeviceDisconnected(int lDeviceID)
        {
            deviceController.Deactivate();
            CSpmDeviceInfo devInfo = deviceController.get_DeviceInfoFromId(lDeviceID);
            int num = devInfo.LFHNumber;
            if (num > 5000 && num < 5999)
                deviceConnected(this, new buttonArgs(lDeviceID.ToString()));
            unitID = 0;
        }

        void deviceController_SPMEventDeviceConnected(int lDeviceID)
        {
            deviceController.Activate();
            CSpmDeviceInfo devInfo = deviceController.get_DeviceInfoFromId(lDeviceID);
            int num = devInfo.LFHNumber;
            if (num > 5000 && num < 5999)
                deviceConnected(this, new buttonArgs(lDeviceID.ToString()));

            unitID = lDeviceID;
        }

        public void LightsOff()
        {
            if (unitID != 0)
            {
                deviceController.SetLED(unitID, 1, spmLEDColor.spmLEDRed, spmLEDState.spmAllLEDsOff);
                deviceController.SetLED(unitID, 1, spmLEDColor.spmLEDGreen, spmLEDState.spmAllLEDsOff);
            }

        }

        public void RedLight(bool IsRecording)
        {
            if (unitID != 0)
                if (IsRecording)
                    deviceController.SetLED(unitID, 1, spmLEDColor.spmLEDRed, spmLEDState.spmLEDOn);
                else
                    deviceController.SetLED(unitID, 1, spmLEDColor.spmLEDRed, spmLEDState.spmAllLEDsOff);
        }

        public void GreenLight(bool IsPlaying)
        {
            if (unitID != 0)
                if (IsPlaying)
                    deviceController.SetLED(unitID, 1, spmLEDColor.spmLEDGreen, spmLEDState.spmLEDOn);
                else
                    deviceController.SetLED(unitID, 1, spmLEDColor.spmLEDGreen, spmLEDState.spmAllLEDsOff);
        }

        public void OffLights()
        {
            if (unitID != 0)
                deviceController.SetLED(unitID, 1, spmLEDColor.spmLEDRed, spmLEDState.spmAllLEDsOff);
        }

        public void FreeManager()
        {
            deviceController.Deactivate();
            deviceController.Deinitialize();
            deviceController = null;
        }

        public void RecordModeLight(int p)
        {
            if (p == 0)
                deviceController.SetLED(unitID, 2, spmLEDColor.spmLEDGreen, spmLEDState.spmLEDOn);
            else
                deviceController.SetLED(unitID, 2, spmLEDColor.spmLEDRed, spmLEDState.spmLEDOn);
        }
    }

    public class buttonArgs
    {
        string btn;
        public buttonArgs(string _button)
        {
            btn = _button;
        }

        public string button
        {
            get { return btn; }
        }
    }

    enum DeviceConnection
    {
        Connected,
        Disconected
    };
}
